package com.wangyb.multitenant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysRoleMenu;
import com.wangyb.multitenant.domain.MtTemplateMenu;
import com.wangyb.multitenant.mapper.MtTemplateMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wangyb.multitenant.bo.MtTemplateAddBo;
import com.wangyb.multitenant.bo.MtTemplateQueryBo;
import com.wangyb.multitenant.bo.MtTemplateEditBo;
import com.wangyb.multitenant.domain.MtTemplate;
import com.wangyb.multitenant.mapper.MtTemplateMapper;
import com.wangyb.multitenant.vo.MtTemplateVo;
import com.wangyb.multitenant.service.IMtTemplateService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 多租户模板Service业务层处理
 *
 * @author wangyb
 * @date 2021-06-23
 */
@Service
public class MtTemplateServiceImpl extends ServiceImpl<MtTemplateMapper, MtTemplate> implements IMtTemplateService {
	@Autowired
	private MtTemplateMenuMapper mtTemplateMenuMapper;

    @Override
    public MtTemplateVo queryById(Long id){
        return getVoById(id, MtTemplateVo.class);
    }

    @Override
    public TableDataInfo<MtTemplateVo> queryPageList(MtTemplateQueryBo bo) {
        PagePlus<MtTemplate, MtTemplateVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MtTemplateVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MtTemplateVo> queryList(MtTemplateQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MtTemplateVo.class);
    }

    private LambdaQueryWrapper<MtTemplate> buildQueryWrapper(MtTemplateQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MtTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MtTemplate::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MtTemplateAddBo bo) {
        MtTemplate add = BeanUtil.toBean(bo, MtTemplate.class);
        validEntityBeforeSave(add);
		Boolean success = save(add);
		insertMtTemplateMenu(bo,add);
        return success;
    }

    @Override
    public Boolean updateByEditBo(MtTemplateEditBo bo) {
        MtTemplate update = BeanUtil.toBean(bo, MtTemplate.class);
        validEntityBeforeSave(update);
		boolean updated = updateById(update);
		// 删除角色与菜单关联
		mtTemplateMenuMapper.delete(new LambdaQueryWrapper<MtTemplateMenu>().eq(MtTemplateMenu::getTemplateId, update.getId()));
		MtTemplateAddBo add = BeanUtil.toBean(bo, MtTemplateAddBo.class);
		insertMtTemplateMenu(add,update);
        return updated;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MtTemplate entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
	/**
	 * 新增租户模板菜单信息
	 *
	 * @param template 租户模板
	 */
	public int insertMtTemplateMenu(MtTemplateAddBo templateAddBo,MtTemplate template) {
		int rows = 1;
		// 新增用户与角色管理
		List<MtTemplateMenu> list = new ArrayList<MtTemplateMenu>();
		for (Long menuId : templateAddBo.getMenuIds()) {
			MtTemplateMenu mtTemplateMenu = new MtTemplateMenu();
			mtTemplateMenu.setTemplateId(template.getId());
			mtTemplateMenu.setMenuId(menuId);
			list.add(mtTemplateMenu);
		}
		if (list.size() > 0) {
			rows = mtTemplateMenuMapper.batchMtTemplateMenu(list);
		}
		return rows;
	}
	/**
	 * 根据租户模板ID查询菜单树信息
	 *
	 * @param templateId 租户模板ID
	 * @return 选中菜单列表
	 */
	@Override
	public List<Integer> selectMenuListByTemplateId(Long templateId) {
		MtTemplate template = baseMapper.selectById(templateId);
		return mtTemplateMenuMapper.selectMenuListByTemplateId(templateId, template.isMenuCheckStrictly());
	}
}
